package ae.pcfc.etks.online.web.controller.register;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.support.RequestContextUtils;

import ae.pcfc.etks.online.common.delegate.ClientRegistrationDelegate;
import ae.pcfc.etks.online.common.vo.TrakheesIdAndPINVO;
import ae.pcfc.etks.online.web.ConstantsAdmin;
import ae.pcfc.etks.online.web.PageType;
import ae.pcfc.etks.online.web.ViewPath;
import ae.pcfc.etks.online.web.ViewUtils;
import ae.pcfc.etks.online.web.controller.BaseController;
import ae.pcfc.etks.online.web.model.Register;
import ae.pcfc.etks.online.web.util.SecurityUtils;

/**
 * 
 * 
 * @author Mohtisham.Salahria
 */
@Controller
@RequestMapping(ViewPath.REGISTER_NEWCLIENT)
public class TKIDController extends BaseController{

	
	private ClientRegistrationDelegate clientRegistrationDelegate;

	@Resource(name="clientRegistrationDelegate")
	public void setClientRegistrationDelegate(
			ClientRegistrationDelegate clientRegistrationDelegate) {
		this.clientRegistrationDelegate = clientRegistrationDelegate;
	}



	/**
	 * Custom handler for displaying the form, i.e.: GET requests for
	 * /register/form.jsp
	 * @see #form(HttpServletRequest,SessionStatus,Register, BindingResult)
	 * @see WEB-INF/jsp/register/form.jsp
	 */
	@RequestMapping(value = "/form.htm", method = RequestMethod.GET)
	public void form(
			@ModelAttribute(value = ConstantsAdmin.REGISTER_MODEL_KEY) Register command) {
		

	}

	/**
	 * Custom handler for displaying the form, i.e.: POST requests for
	 * /register/form.jsp
	 * @see #form(Register)
	 * @see WEB-INF/jsp/register/form.jsp
	 */
	
	
	@RequestMapping(method = RequestMethod.POST, value = "/form.htm")
	public String form(
			HttpServletRequest request,HttpServletResponse response,
			SessionStatus status,
			@ModelAttribute(value = ConstantsAdmin.REGISTER_MODEL_KEY) @Valid Register clientRegisterModel,
			BindingResult errors) throws Exception {

			if (errors.hasErrors()) {			
				
				return ViewUtils.DEFAULT_VIEW;
			}
			else 
			{
				//businessDelegate.registerCompany();
				
				TrakheesIdAndPINVO idAndPINVO = new TrakheesIdAndPINVO();
				
				getMapper().map(clientRegisterModel, idAndPINVO);
				
				if(clientRegistrationDelegate.validateTrakheesIdAndPIN(idAndPINVO)){
											
					Map<String, String> paramMap = new HashMap<String, String>();
					paramMap.put("clientId", SecurityUtils.encrypt(idAndPINVO.getStrTrakheesID()));	
					
					String[] trakheesId = idAndPINVO.getStrTrakheesID().split("-");
					
					if(trakheesId[0].endsWith("P"))
					return ViewUtils.getRedirect(ViewPath.CLIENT_PERSON_REGISTER, PageType.FORM, paramMap);
					if(trakheesId[0].endsWith("C"))
					return ViewUtils.getRedirect(ViewPath.CLIENT_COMPANY_REGISTER, PageType.FORM, paramMap);
				}
				
				String[] messageArgs = new String[] { getMessageSource().getMessage("label.company.uploaddocuments", null,
						RequestContextUtils.getLocale(request)) };
				errors.reject("error.trakheesId.pin.invalid", messageArgs, null);

				return ViewUtils.getForward(ViewPath.REGISTER_NEWCLIENT, PageType.FORM);
			}
		
	}
}
